from abc import ABC, abstractmethod

from PyQt5.QtCore import pyqtSignal, Qt
from PyQt5.QtWidgets import (QApplication, QWidget, QComboBox, QDialog, QLabel,
        QDialogButtonBox, QFormLayout, QGridLayout, QGroupBox, QHBoxLayout,
        QVBoxLayout, QLineEdit, QMenu, QMenuBar, QPushButton, QSpinBox, QTextEdit,
        QRadioButton, QMessageBox)

from config import *
from kxml import KXml

LOG = make_logging("kwidget")


class KWidget(ABC):
    """ 图形控件抽象基类，并提供图元操作的基础方法 """
    def __init__(self, dict_widget, parent=None):
        self.widget = QWidget()  # 组合核心元素：Qt 图元
        self.attrib = dict_widget
        if parent is not None:
            self.create_qt_obj(parent)

    def run(self):
        self.set_attr_common()
        self.set_attr_unique()

    @abstractmethod
    def create_qt_obj(self, parent):
        """ 实例化一个Qt对象 """
        pass

    def get_qt_obj(self):
        """ 返回 Qt 原始图元对象，用于直接调用qt对象的属性与方法 """
        return self.widget

    def copy_self_obj(self):
        pass

    def paste_obj(self):
        pass

    def keyPressEvent(self, event):
        if event.modifiers() == Qt.CtrlModifier and event.key() == Qt.Key_C:
            LOG.debug("Copy Widget Object")
            self.copy_self_obj()
        elif event.modifiers() == Qt.CtrlModifier and event.key() == Qt.Key_V:
            LOG.debug("Paste Widget Object")
            self.paste_obj()

    def close(self):
        self.widget.close()

    def set_attr_common(self):
        """ 设置通用属性 """
        for attr in Set_Widgets_attrib_common:
            value = self.attrib[attr]
            if attr == "name":
                self.widget.setAccessibleName(value)
            elif attr == "pos":
                num = switch_str2namedtuple(CoupleNum, value)
                self.widget.move(num.x, num.y)
            elif attr == "size":
                num = switch_str2namedtuple(CoupleNum, value)
                self.widget.resize(num.x, num.y)
            else:
                Exception("Not define the default action of attribute 【%s】" % attr)

    def set_attr_unique(self):
        """ 设置每个类型的特殊属性 """
        pass

    def format_xml2gui(self):
        """ 格式转换，从xml文件的文本转为对象 """
        pass
    
    def format_gui2xml(self):
        """ 格式转换，从对象导出为xml """
        pass


class PushButton(KWidget):
    def __init__(self, dict_widget, parent=None):
        super().__init__(dict_widget, parent)

    def create_qt_obj(self, parent):
        LOG.debug("The type of Widget is : 【%s】" % type(self))
        self.widget = QPushButton(parent)


class Text(KWidget):
    def __init__(self, dict_widget, parent=None):
        super().__init__(dict_widget, parent)

    def create_qt_obj(self, parent):
        self.widget = QTextEdit(parent)

    def set_attr_unique(self):
        self.set_font()
        self.set_text_size()

    def set_font(self):
        pass

    def set_text_size(self):
        pass
